package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.FlinkResourceResponse;
import com.sh.data.engine.application.model.dto.datadev.UploadFlinkResourceResponse;
import com.sh.data.engine.application.model.request.datadev.SaveResourceRequest;
import com.sh.data.engine.application.service.datadev.FlinkResourceApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.datadev.flink.service.FlinkNodeConfigService;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/flink")
@Tag(name = "数据开发-实时开发-资源管理", description = "实时计算资源管理API")
@Slf4j
public class FlinkResourceController {

    @Autowired
    private FlinkResourceApplicationService flinkResourceApplicationService;

    @Autowired
    private FlinkNodeConfigService flinkNodeConfigService;

    /**
     * 实时采集资源上传
     *
     * @param file
     * @return
     */
    @PostMapping("/resource/upload")
    @Operation(summary = "实时采集上传文件")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.UPLOAD)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<UploadFlinkResourceResponse> uploadResource(
        @RequestPart("file") MultipartFile file) {
        var result = flinkResourceApplicationService.uploadResource(file);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/saveResourceNode")
    @Operation(summary = "保存资源节点")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.实时任务资源)
//    @Log(
//        business = Business.实时任务资源,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> saveFlinkResource(
        @Valid @RequestBody SaveResourceRequest saveResourceRequest) {
        var result = flinkResourceApplicationService.saveFlinkResource(saveResourceRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/resource/detail")
    @Operation(summary = "获取资源详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<FlinkResourceResponse> getFlinkResourceResponseDetail(
        @Parameter(description = "节点id", required = true) @RequestParam Long nodeId) {
        var result = flinkResourceApplicationService.getFlinkResourceResponseDetail(nodeId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/resource/list")
    @Operation(summary = "获取资源详情列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<PageResult<FlinkResourceResponse>> getFlinkResourceResponseList(
        @Parameter(description = "节点id", required = true) @RequestParam Long nodeId,
        @Parameter(description = "页数", required = false, schema = @Schema(defaultValue = "1"))
        @RequestParam(required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "每页大小", required = false, schema = @Schema(defaultValue = "10"))
        @RequestParam(required = false, defaultValue = "10")
        Integer pageSize) {
        var result = flinkResourceApplicationService.getFlinkResourceResponseList(nodeId, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/resource/edit")
    @Operation(summary = "编辑资源名称")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> editFlinkResourceByNodeId(
        @Parameter(description = "节点NodeId", required = true) @RequestParam Long nodeId,
        @Parameter(description = "资源名称", required = true) @RequestParam String resourceName) {
        var result = flinkResourceApplicationService.editFlinkResourceByNodeId(nodeId, resourceName);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/resource/drop")
    @Operation(summary = "删除资源")
    @LogExecution(operation = EngineOperation.DEL, business = Business.实时任务资源)
//    @Log(
//        business = Business.实时任务资源,
//        operation = Operation.DEL)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> dropFlinkResourceById(
        @Parameter(description = "id", required = true) @RequestParam Long nodeId) {
        var result = flinkResourceApplicationService.dropFlinkResourceById(nodeId);
        return RestResponseEntity.success(result);
    }
}
